package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    //获取当前用户已经设置的地址
    @GetMapping("/list")
    public R<List<AddressBook>> list() {
        LambdaQueryWrapper<AddressBook> addressBookWrapper = new LambdaQueryWrapper<>();
        addressBookWrapper.eq(BaseContext.get() != null, AddressBook::getUserId, BaseContext.get());
        addressBookWrapper.orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> addressBookList = addressBookService.list(addressBookWrapper);
        return R.success(addressBookList);
    }

    //设置默认地址
    @PutMapping("/default")
    public R<String> setDefaultAddress(@RequestBody AddressBook addressBook) {
        LambdaUpdateWrapper<AddressBook> addressBookWrapper = new LambdaUpdateWrapper<>();
        addressBookWrapper.eq(AddressBook::getUserId, BaseContext.get()).set(AddressBook::getIsDefault, 1);
        addressBookService.update(addressBookWrapper);
        return R.success("设置成功！！");
    }
    //添加地址


}
